<%--
  Created by IntelliJ IDEA.
  User: 86133
  Date: 2025/5/2
  Time: 10:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>类型列表</title>
    <base href="${basePath}">
    <link rel="stylesheet" href="static/plugins/layui/css/layui.css">
    <script src="static/plugins/jquery-3.1.0.js"></script>
    <script src="static/plugins/layui/layui.js"></script>
    <style>
        .required{
            color:red;
            margin:0 5px
        }
    </style>
</head>
<body  style="padding: 15px;">
<div class="layui-container">
    <form class="layui-form" id="dataForm" lay-filter="dataForm">
        <div class="layui-form-item">
            <label for="name" class="layui-form-label"><span class="required">*</span>分类名称</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input layui-inline" name="name" id="name" placeholder="请输入分类名称" lay-verify="required" lay-request="分类名称不能为空">
            </div>

        </div>
        <div class="layui-form-item">
            <label for="profile" class="layui-form-label">分类介绍</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="profile" id="profile" placeholder="请输入详细介绍"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="status" class="layui-form-label"><span class="required">*</span>状态</label>
            <div class="layui-input-block">
                <select name="status" id="status" lay-verify="required" lay-request="状态不可为空">
                    <option value="1">可用</option>
                    <option value="2">禁用</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="save" lay-submit>提交</button>
            </div>
        </div>
    </form>
</div>
</body>
<script>
   var form;
   const dataId = sessionStorage.getItem("dataId") || "";
   $(function (){
       layui.use(function (){
           form = layui.form;
           getFormData();
           form.on("submit(save)",function ({field}){
               $.ajax({
                   url:"category",
                   data:{
                       param:"update",
                       id:dataId,
                       ...field
                   },
                   method: "post",
                   dataType:"json",
                   success(res){
                       if(res.code === 200){
                           layer.msg("操作成功！",{icon:6},
                               function (){
                                   let index = parent.layer.getFrameIndex(window.name);
                                   parent.layer.close(index);
                                   parent.table.reload('dataTable');
                               });
                       }else{
                           layer.alert(res.msg);
                       }
                   }
               })
               return false;
           })
       })
   })

   function getFormData(){
       if(dataId){
           sessionStorage.removeItem("dataId");
           $.ajax({
               url:"category",
               data:{
                   param:"info",
                   id:dataId
               },
               method:"get",
               success(res){
                   if(res.code === 200){
                       form.val('dataForm',res.data);
                   }
               }
           })
       }
   }
</script>
</html>
